
// Copyright (C) 2007 Richard Uhler
// This file is part of Esiply.
//
// Esiply is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, Version 2,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <assert.h>

#include "HostFunction.h"
#include "HostValue.h"
#include "Number.h"
#include "String.h"
#include "Undefined.h"
#include "Value.h"

HostValue::HostValue()
    : m_value(Undefined::undefined)
{}

HostValue::HostValue(Value* val)
    : m_value(val)
{}

HostValue::HostValue(const HostValue& rhs)
    : m_value(rhs.m_value)
{}

HostValue::HostValue(HostContext context, const char* params,
        host_function_t fp)
    : m_value(new HostFunction(context, params, fp))
{}

HostValue::HostValue(double val)
    : m_value(new Number(val))
{}

HostValue::HostValue(const char* str)
    : m_value(new String(str))
{}

HostValue::HostValue(const char* str, int len)
    : m_value(new String(str, len))
{}

const char* HostValue::c_str() const
{
    // we aren't implementing non-string types yet
    assert(m_value->getType() == VT_STRING);
    return m_value->string()->c_str();
}

HostValueType HostValue::type() const
{
    // we aren't implementing non-string types yet
    assert(m_value->getType() == VT_STRING);
    return HT_STRING;
}

Value* HostValue::get_value() const
{
    return m_value;
}

void HostValue::set_value(Value* val)
{
    m_value = val;
}

std::ostream& operator<<(std::ostream& outs, const HostValue& rhs)
{
    // Here we just convert the value to a string, and output that.
    outs << *rhs.m_value->toString()->string();
    return outs;
}

bool HostValue::isException() const
{
    return m_value->isException();
}

